package world.snowcrystal.aop;


import lombok.extern.log4j.Log4j2;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.redisson.api.RBloomFilter;
import org.springframework.core.annotation.Order;
import org.springframework.security.core.userdetails.UsernameNotFoundException;


@Aspect
@Log4j2
@Order(1)
//@Component
public class UserAuthenticationBloomFilterAspect {

//    @Autowired
    private RBloomFilter<String> bloomFilter;

    @Around("execution( org.springframework.security.core.userdetails.UserDetails loadUserByUsername(String)  )")
    public Object userExist(final ProceedingJoinPoint joinPoint) throws Throwable {
        String username = (String) joinPoint.getArgs()[0];
        if (bloomFilter.contains(username)) {
            log.info("bloom filter : user {} exists, continue lookup.",username);
            return joinPoint.proceed();
        }
        log.info("bloom filter : {} not exist", username);
        throw new UsernameNotFoundException(username);
    }
}



